package com.kgcx.system.service;

import com.kgcx.common.constant.Cons;
import com.kgcx.system.domain.BaseInfo;
import com.kgcx.system.mapper.BaseInfoMapper;
import com.kgcx.system.rabbitmq.producer.BaseInfoProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 吴刚林
 * @date 2017/11/20
 */
@Service
public class BaseInfoService {
    @Autowired
    private BaseInfoMapper baseInfoMapper;

    @Autowired
    private BaseInfoProducer baseInfoProducer;

    /**
     * 根据ID查询公司信息放到MQ里面给监管平台
     * @param kid
     * @return
     */
    public BaseInfo getBaseInfo(Long kid) {
        BaseInfo baseInfo =baseInfoMapper.selectByPrimaryKey(kid);
        baseInfoProducer.addBaseInfo(baseInfo);
      return  baseInfo;
    }

    @Transactional(rollbackFor = Exception.class)
    public void addBaseInfo(BaseInfo baseInfo) {
        baseInfoMapper.insertSelective(baseInfo);

        baseInfoProducer.addBaseInfo(baseInfo);

    }

    @Transactional(rollbackFor = Exception.class)
    public void updateBaseInfo(BaseInfo baseInfo) {
        baseInfoMapper.updateByPrimaryKeySelective(baseInfo);

        baseInfoProducer.updateBaseInfo(baseInfo);
    }

    @Transactional(rollbackFor = Exception.class)
    public void removeBaseInfo(Long kid) {
        BaseInfo baseInfo = new BaseInfo();
        baseInfo.setKid(kid);
        baseInfo.setStatus(Cons.COMMON_STATUS_DELETE);
        baseInfoMapper.updateByPrimaryKeySelective(baseInfo);

        baseInfoProducer.deleteBaseInfo(baseInfo);
    }
}
